package com.example.innospace.demos.web.service.Impl;


import com.example.innospace.demos.pojo.dto.StudentDTO;
import com.example.innospace.demos.pojo.dto.StudentPageQueryDTO;
import com.example.innospace.demos.pojo.entity.Admin;
import com.example.innospace.demos.pojo.entity.Student;
import com.example.innospace.demos.pojo.result.PageResult;
import com.example.innospace.demos.web.mapper.StudentMapper;
import com.example.innospace.demos.web.service.StudentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

   @Autowired
    private StudentMapper studentMapper;
    private Student student;

    /**
     * 学生用户登录
     *
     * @param studentId
     * @return
     */
    @Override
    public Student findById(String studentId) {
        return studentMapper.findById(studentId);
    }

    /**
     * 更新学生信息
     *
     * @param student
     * @return
     */
    @Override
    public boolean update(Student student) {
        Student student1=findById(student.getStudentId());
        if(student1!=null)
        studentMapper.update(student);
        else
            return false;
        return true;
    }

    /**
     * 获取头像
     * @param studentId
     * @return
     */
    @Override
    public String getAvatar(String studentId) {
        return studentMapper.getAvatar(studentId);
    }

    /**
     * 上传头像
     * @param studentId
     * @param fileUrl
     */
    @Override
    public void updateAvatar(String studentId, String fileUrl) {
        studentMapper.updateAvatar(studentId,fileUrl);
    }

    /**
     * 获得学生信息
     * @param studentId
     * @return
     */
    @Override
    public Student getStudentInfo(String studentId) {
        return studentMapper.getStudentInfo(studentId);
    }

    /**
     * 获得密码
     * @param studentId
     * @return
     */
    @Override
    public String getpassword(String studentId) {
        return studentMapper.getpassword(studentId);
    }

    /**
     * 更新密码
     * @param newPassword
     * @param studentId
     */
    @Override
    public void updatePassword(String newPassword, String studentId) {
       studentMapper.updatePassword(newPassword,studentId);
    }

    @Override
    public void insert(StudentDTO studentDTO) {
        studentDTO.setAvatar("https://spacea.oss-cn-beijing.aliyuncs.com/14a40fac-4185-4d94-824f-efe83fcb60f0-195.jpg");
        studentDTO.setStatus(1);
        studentMapper.insert(studentDTO);
        studentMapper.setPassWord("ksd@"+studentDTO.getStudentId(),studentDTO.getStudentId());
    }

    @Override
    public void resetPassword(String studentId) {
        Student student1=studentMapper.findById(studentId);
        String password="ksd@"+student1.getStudentId();
        studentMapper.updatePassword(password,studentId);
    }

    @Override
    public void studentstartOrstop(Integer status, String studentId) {
        studentMapper.studentstartOrstop(status,studentId);
    }

    @Override
    public boolean batchDeleteStudents(List<String> studentIds) {
        for(int i=0;i<studentIds.size();i++)
        {
            Student student1=studentMapper.findById(studentIds.get(i));
            if(student1.getStatus()==1)
                return false;
        }
        studentMapper.batchDeleteStudents(studentIds);
        return true;
    }

    @Override
    public PageResult pageQuery(int pageNum, int pageSize, StudentPageQueryDTO studentPageQueryDTO) {
        PageHelper.startPage(pageNum,pageSize);
        Page<Admin> page=studentMapper.pageQuery(studentPageQueryDTO);
        long total=page.getTotal();
        List<Admin> records=page.getResult();
        return new PageResult(total,records);
    }

    @Override
    public List<Student> findByProjectId(Integer id) {
        return studentMapper.findByprojectId(id);
    }
}
